package lumis.service.resourcescheduler;

import java.util.Calendar;

import lumis.doui.control.datepicker.DateTimePickerControl;
import lumis.portal.PortalException;

/**
 * A control for selecting dateTime values for a schedule.
 * This control uses as a default value for a start dateTime the 
 * next half-hour multiple, and for a end dateTime an hour after
 * the default start dateTime.
 * <p>
 * The dataId is used to identify if this control is being used
 * for a start datetime. If it is not equals to <code>startDateTime</code>,
 * it assumes it is for the end datetime. 
 * @version $Revision: 6376 $ $Date: 2007-04-10 17:55:53 -0300 (Tue, 10 Apr 2007) $
 * @since 4.0.10
 */
public class ScheduleDateTimePickerControl extends DateTimePickerControl
{
	@Override
	protected Object getDefaultValue() throws PortalException
	{
		// get current time, without seconds or milliseconds
		Calendar start = Calendar.getInstance();
		start.set(Calendar.SECOND, 0);
		start.set(Calendar.MILLISECOND, 0);
		
		int hour = start.get(Calendar.HOUR);
		int minute = start.get(Calendar.MINUTE);
		
		if (minute < 30)
		{
			// set the minute to the next 30 minutes
			start.set(Calendar.MINUTE, 30);
		}
		else
		{
			// set the time to the start of next hour
			hour++;
			start.set(Calendar.HOUR, hour);
			start.set(Calendar.MINUTE, 00);
		}

		// if dataId is not startDateTime, assume this control is
		// for endDateTime and add an hour to the default value
		// calculated for the startDateTime
		if(!"startDateTime".equals(getDataId()))
			start.set(Calendar.HOUR, hour + 1);

		return start.getTime();
	}
}
